/**
 * @author qianrong.yin
 * @description 全局的路由控制文件
 * @create 2021-12-07
 */

// 加载Vue
import Vue from 'vue'
// 加载路由
import Router from 'vue-router'
import store from '../store'

// 引入页面组件
import Login from '../views/login'
import Home from '../views/home'
import NotFound from '../views/common/NotFound' // 404页面

import ProjectManagement from '../views/project' // 项目管理
import ProjectEdit from '../views/project/Edit' // 新建项目

import PurchaseManagement from '../views/purchase' // 采购管理
import PurchaseEdit from '../views/purchase/Edit' // 新建采购
import PurchaseDetail from '../views/purchase/Detail' // 详情

import Setting from '../views/setting' // 设置
// 实例化
Vue.use(Router)

const router = new Router({
  // 模式配置
  mode: 'history',
  // 配置路由
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login, meta: { notLogin: true } },
    {
      path: '/home',
      name: 'myhome',
      component: Home
    },
    { path: '/project/list', component: ProjectManagement },
    { path: '/project/edit', name: 'editProject', component: ProjectEdit },
    { path: '/purchase/list', component: PurchaseManagement },
    { path: '/purchase/edit', component: PurchaseEdit },
    { path: '/purchase/detail', component: PurchaseDetail },
    { path: '/setting', component: Setting },
    { path: '*', component: NotFound }
  ]
})

// 路由权限拦截 ---没有登录的用户不能访问一些内部业务
router.beforeEach(function (to, from, next) {
  if (to.meta.notLogin) {
    next()
    return
  }

  // 检测登录标识，如果有则继续业务，没有则重定向到登录业务
  // const token = 'token'
  const token = store.getters['common/token']
  if (token) {
    next()
  } else {
    next('./login')
  }
})

export default router
